//
//  YLASetViewControll.swift
//  Your Lover AI
//
//  Created by 阿忠 on 2024/6/24.
//

import Foundation
import UIKit
import PopupDialog

class YLASetViewControll: UIViewController{
    
    var window: UIWindow?
    var title_lb: UILabel!      // 标题
    var nameView: YLANameView! = nil
    var blurEffectView: UIVisualEffectView! = nil
   
    lazy var bgImg: UIImageView = {
        let image = UIImageView(image: UIImage(named: "bg"))
        return image
    }()
    
    //修改图
    lazy var editImg: UIImageView = {
        let image = UIImageView(image: UIImage(named: "set_edit"))
        return image
    }()
    
    //提示
    lazy var titleLab: UILabel = {
        let lab = UILabel()
        lab.text = "Your nickname"
        lab.textColor = UIColor(hex: "FFFFFF")
        lab.font = UIFont.systemFont(ofSize: 15)
        lab.textAlignment = .left
        lab.numberOfLines = 0
        return lab
    }()
    
    //按钮背景
    lazy var editView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex: "7157FF")
        view.layer.cornerRadius = 10.0 // 设置圆角半径为10.0
        view.layer.masksToBounds = true
        return view
    }()
    
    //名字
    lazy var nameLab: UILabel = {
        let lab = UILabel()
        lab.text = "Your nickname"
        lab.textColor = UIColor(hex: "FFFFFF")
        lab.font = UIFont.systemFont(ofSize: 17)
        lab.textAlignment = .left
        lab.numberOfLines = 0
        return lab
    }()
    
    lazy var nameBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.backgroundColor = .clear
        btn.addTarget(self, action: #selector(nameClick), for: .touchUpInside)
        self.view.addSubview(btn)
        return btn
    }()
    
    //名字
    lazy var genderLab: UILabel = {
        let lab = UILabel()
        lab.text = "Your nickname"
        lab.textColor = UIColor(hex: "FFFFFF")
        lab.font = UIFont.systemFont(ofSize: 17)
        lab.textAlignment = .left
        lab.numberOfLines = 0
        return lab
    }()
    
    //女生背景
    lazy var femaleView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex: "7157FF")
        view.layer.cornerRadius = 10.0 // 设置圆角半径为10.0
        view.layer.masksToBounds = true
        return view
    }()
    
    //女生图
    lazy var femaleImg: UIImageView = {
        let image = UIImageView(image: UIImage(named: "set_female"))
        return image
    }()
    
    //女生选择图
    lazy var femaleSelImg: UIImageView = {
        let image = UIImageView(image: UIImage(named: "set_sel"))
        return image
    }()
    
    //女生提示
    lazy var femaleLab: UILabel = {
        let lab = UILabel()
        lab.text = "Female"
        lab.textColor = UIColor(hex: "FFFFFF")
        lab.font = UIFont.systemFont(ofSize: 17)
        lab.textAlignment = .left
        lab.numberOfLines = 0
        return lab
    }()
    
    //女生选择
    lazy var femaleBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .clear
        button.addTarget(self, action: #selector(femaleClick), for: .touchUpInside)
        return button
    }()
    
    //男生背景
    lazy var maleView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex: "7157FF")
        view.layer.cornerRadius = 10.0 // 设置圆角半径为10.0
        view.layer.masksToBounds = true
        return view
    }()
    
    //男生图
    lazy var maleImg: UIImageView = {
        let image = UIImageView(image: UIImage(named: "set_male"))
        return image
    }()
    
    //男生提示
    lazy var maleLab: UILabel = {
        let lab = UILabel()
        lab.text = "Male"
        lab.textColor = UIColor(hex: "FFFFFF")
        lab.font = UIFont.systemFont(ofSize: 17)
        lab.textAlignment = .left
        lab.numberOfLines = 0
        return lab
    }()
    
    //男生选择图
    lazy var maleSelImg: UIImageView = {
        let image = UIImageView(image: UIImage(named: "set_sel"))
        return image
    }()
    
    //女生选择
    lazy var maleBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .clear
        button.addTarget(self, action: #selector(maleClick), for: .touchUpInside)
        return button
    }()
    
    //其他背景
    lazy var otherView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex: "7157FF")
        view.layer.cornerRadius = 10.0 // 设置圆角半径为10.0
        view.layer.masksToBounds = true
        return view
    }()
    
    //其他提示
    lazy var otherLab: UILabel = {
        let lab = UILabel()
        lab.text = "Other"
        lab.textColor = UIColor(hex: "FFFFFF")
        lab.font = UIFont.systemFont(ofSize: 17)
        lab.textAlignment = .left
        lab.numberOfLines = 0
        return lab
    }()
    
    //其他选择图
    lazy var otherSelImg: UIImageView = {
        let image = UIImageView(image: UIImage(named: "set_sel"))
        return image
    }()
    
    //女生选择
    lazy var otherBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .clear
        button.addTarget(self, action: #selector(otherClick), for: .touchUpInside)
        return button
    }()
    
    lazy var nextBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.backgroundColor = UIColor(hex: "19CC70")
        btn.setTitle("Continue", for: .normal)
        btn.layer.masksToBounds = true
        btn.layer.cornerRadius = 25
        btn.setTitleColor(UIColor.white, for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 17)
        btn.addTarget(self, action: #selector(nextClick), for: .touchUpInside)
        return btn
    }()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = UIColor.red
        
 
        setUI()
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.tabBarController!.tabBar.isHidden = true
    }
    
    func setUI() {
        
        view.addSubview(bgImg)
        bgImg.snp.makeConstraints { make in
            make.left.top.bottom.right.equalToSuperview()
        }
        
        title_lb = UILabel()
        title_lb.textColor = .white
        title_lb.font = UIFont.boldSystemFont(ofSize: 25)
        title_lb.textAlignment = .left
        title_lb.text = "Set"
        view.addSubview(title_lb)
        title_lb.snp.makeConstraints { make in
            make.left.equalTo(17)
            make.top.equalTo(55)
            make.height.equalTo(25)
        }
        
        view.addSubview(titleLab)
        view.addSubview(editView)
        editView.addSubview(nameLab)
        editView.addSubview(editImg)
        
        titleLab.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.top.equalTo(title_lb.snp.bottom).offset(26.5)
            make.height.equalTo(15)
        }
        
        editView.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.top.equalTo(titleLab.snp.bottom).offset(15)
            make.height.equalTo(56)
        }
        
        nameLab.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.centerY.equalToSuperview()
        }
        
        editImg.snp.makeConstraints { make in
            make.right.equalTo(-16)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(29)
        }
        
        nameBtn.snp.makeConstraints { make in
            make.left.top.right.bottom.equalTo(editView)
        }
        
        self.view.addSubview(genderLab)
        genderLab.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.top.equalTo(editView.snp.bottom).offset(22)
            make.height.equalTo(15)
        }
        
        self.view.addSubview(femaleView)
        femaleView.addSubview(femaleImg)
        femaleView.addSubview(femaleLab)
        femaleView.addSubview(femaleSelImg)
        femaleView.addSubview(femaleBtn)
        self.view.addSubview(maleView)
        maleView.addSubview(maleImg)
        maleView.addSubview(maleLab)
        maleView.addSubview(maleSelImg)
        maleView.addSubview(maleBtn)
        self.view.addSubview(otherView)
        otherView.addSubview(otherLab)
        otherView.addSubview(otherSelImg)
        otherView.addSubview(otherBtn)
        
        let screenSize = (UIScreen.main.bounds.size.width - 48 ) / 2
        
        femaleView.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.top.equalTo(genderLab.snp.bottom).offset(15)
            make.height.equalTo(56)
            make.width.equalTo(screenSize)
        }
        
        femaleImg.snp.makeConstraints { make in
            make.left.equalTo(31)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(29)
        }
        
        femaleLab.snp.makeConstraints { make in
            make.left.equalTo(femaleImg.snp.right).offset(5)
            make.centerY.equalToSuperview()
        }
        
        femaleSelImg.snp.makeConstraints { make in
            make.top.right.equalToSuperview()
            make.width.equalTo(22)
            make.height.equalTo(20)
        }
        
        femaleBtn.snp.makeConstraints { make in
            make.left.top.right.bottom.equalToSuperview()
        }
        
        maleView.snp.makeConstraints { make in
            make.right.equalTo(-16)
            make.top.equalTo(femaleView)
            make.height.equalTo(56)
            make.width.equalTo(screenSize)
        }
        
        maleImg.snp.makeConstraints { make in
            make.left.equalTo(31)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(29)
        }
        
        maleLab.snp.makeConstraints { make in
            make.left.equalTo(maleImg.snp.right).offset(5)
            make.centerY.equalToSuperview()
        }
        
        maleSelImg.snp.makeConstraints { make in
            make.top.right.equalToSuperview()
            make.width.equalTo(22)
            make.height.equalTo(20)
        }
        
        maleBtn.snp.makeConstraints { make in
            make.left.top.right.bottom.equalToSuperview()
        }
        
        otherView.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.top.equalTo(femaleView.snp.bottom).offset(16)
            make.height.equalTo(56)
        }
        
        otherLab.snp.makeConstraints { make in
            make.centerX.centerY.equalToSuperview()
        }
        
        otherSelImg.snp.makeConstraints { make in
            make.top.right.equalToSuperview()
            make.width.equalTo(22)
            make.height.equalTo(20)
        }
        
        otherBtn.snp.makeConstraints { make in
            make.left.top.right.bottom.equalToSuperview()
        }
      
        femaleView.layer.borderWidth = 3
        femaleView.layer.borderColor = UIColor.clear.cgColor
        maleSelImg.isHidden = true
        otherSelImg.isHidden = true
        femaleSelImg.isHidden = true
        
        self.view.addSubview(nextBtn)
        nextBtn.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.bottom.equalTo(-35)
            make.height.equalTo(52)
        }
   
    }
    
    
    @objc func nameClick() {
        print("--------设置页修改名字---------")
        let blurEffect = UIBlurEffect(style: .light) // 可调整模糊效果的样式
        self.blurEffectView = UIVisualEffectView(effect: blurEffect)
        self.blurEffectView.frame = self.view.bounds
        self.view.addSubview(self.blurEffectView)
        
        let dialogFrame = CGRect(x: 0, y: 200, width: 270, height: 200)
        self.nameView = YLANameView(frame: dialogFrame, title: "续费", paymentMethod: "支付宝", paymentDescription: "请使用支付宝完成支付。")
        
        self.view.addSubview(self.nameView)
        self.nameView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(200)
            make.left.equalTo(43)
            make.right.equalTo(-43)
            make.height.equalTo(165)
        }
        
        self.view.insertSubview(self.blurEffectView, belowSubview: self.nameView)
        
        self.nameView.onCancel = {
            // 执行取消操作
            self.nameView.removeFromSuperview()
            self.blurEffectView.removeFromSuperview()
        }

        nameView.onConfirm = { [self] text in
            if let password = text {
                print("您输入的支付密码是：\(password)")
                //存储用户登录状态
                let  user_name = password
                let userDefault = UserDefaults.standard
                userDefault.set(user_name, forKey: "user_name")
                nameLab.text = password
                self.nameView.removeFromSuperview()
                self.blurEffectView.removeFromSuperview()
                
            } else {
                print("取消了输入")
            }
        }
      
    }
  
    @objc func femaleClick(){
        
        femaleView.layer.borderWidth = 3
        femaleView.layer.borderColor = UIColor.white.cgColor
        femaleSelImg.isHidden = false
        
        maleView.layer.borderWidth = 3
        maleView.layer.borderColor = UIColor.clear.cgColor
        maleSelImg.isHidden = true
        
        otherView.layer.borderWidth = 3
        otherView.layer.borderColor = UIColor.clear.cgColor
        otherSelImg.isHidden = true
        
        let  user_gender = "female"
        let userDefault = UserDefaults.standard
        userDefault.set(user_gender, forKey: "user_gender")
    }
    
    @objc func maleClick(){
        
        femaleView.layer.borderWidth = 3
        femaleView.layer.borderColor = UIColor.clear.cgColor
        femaleSelImg.isHidden = true
        
        maleView.layer.borderWidth = 3
        maleView.layer.borderColor = UIColor.white.cgColor
        maleSelImg.isHidden = false
        
        otherView.layer.borderWidth = 3
        otherView.layer.borderColor = UIColor.clear.cgColor
        otherSelImg.isHidden = true
        
        let  user_gender = "male"
        let userDefault = UserDefaults.standard
        userDefault.set(user_gender, forKey: "user_gender")
    }
    
    @objc func otherClick(){
        
        femaleView.layer.borderWidth = 3
        femaleView.layer.borderColor = UIColor.clear.cgColor
        femaleSelImg.isHidden = true
        
        maleView.layer.borderWidth = 3
        maleView.layer.borderColor = UIColor.clear.cgColor
        maleSelImg.isHidden = true
        
        otherView.layer.borderWidth = 3
        otherView.layer.borderColor = UIColor.white.cgColor
        otherSelImg.isHidden = false
        
        let  user_gender = "other"
        let userDefault = UserDefaults.standard
        userDefault.set(user_gender, forKey: "user_gender")
    }
    
    @objc func nextClick() {
        print("下一步")
        
        if let isName = UserDefaults.standard.string(forKey: "user_name") {
            // isGender 有一个值，可以在这里使用它
            print("User gender is: \(isName)")
        } else {
            let popup = PopupDialog(title: "Notice", message: "Enter Name", image: nil)
            self.present(popup, animated: true, completion: nil)
            return
        }
        
        if let isGender = UserDefaults.standard.string(forKey: "user_gender") {
            // isGender 有一个值，可以在这里使用它
            print("User gender is: \(isGender)")
        } else {
            let popup = PopupDialog(title: "Notice", message: "Selective Sex", image: nil)
            self.present(popup, animated: true, completion: nil)
            return
        }
        
        let  user_set = "user_set"
        let userDefault = UserDefaults.standard
        userDefault.set(user_set, forKey: "user_set")
        
        let homeVc = YLAHomeViewController()
        let nav_1 = UINavigationController(rootViewController: homeVc)
        var image:UIImage = UIImage(named: "tabbar_home_s")!
        var selectedimage:UIImage = UIImage(named: "tabbar_home_u")!
        image = image.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
        selectedimage = selectedimage.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
        nav_1.tabBarItem = UITabBarItem(title: "Home", image: selectedimage, selectedImage: image)
        
        let appforVc = YLAListViewController()
        let nav_2 = UINavigationController(rootViewController: appforVc)
        var image_2:UIImage = UIImage(named: "tabbar_chat_u")!
        var selectedimage_2:UIImage = UIImage(named: "tabbar_chat_s")!
        image_2 = image_2.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
        selectedimage_2 = selectedimage_2.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
        nav_2.tabBarItem = UITabBarItem(title: "Chat", image: image_2, selectedImage: selectedimage_2)
        
        let setVc = YLAMyViewController()
        let nav_3 = UINavigationController(rootViewController: setVc)
        var image_3:UIImage = UIImage(named: "tabbar_me_u")!
        var selectedimage_3:UIImage = UIImage(named: "tabbar_me_s")!
        image_3 = image_3.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
        selectedimage_3 = selectedimage_3.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
        nav_3.tabBarItem = UITabBarItem(title: "Me", image: image_3, selectedImage: selectedimage_3)
        
        // 全局设置TabBarItem的标题文字颜色（可选）
        self.tabBarController!.tabBar.tintColor = UIColor.white
        
        self.tabBarController!.tabBar.unselectedItemTintColor = UIColor(hex: "D36AF0")
        
        self.tabBarController!.viewControllers = [nav_1, nav_2, nav_3]
    }
}
